<template>
	<view class="scrollPage">
		<u-navbar :border-bottom="false" title="上传海报" back-icon-color="#ffffff" :background="{background:'#2149c3'}"
		 title-color="#ffffff"></u-navbar>
		 <view class="padding">
			<view class="tj_box">
				<view>是否推荐：</view>
				<u-switch v-model="value" @change="change" inactiveColor="rgb(230, 230, 230)"></u-switch>
			</view>  
			<view class="solids-bottom">
				<u-button type="primary" text="上传" @click="goPosterUpload()">上传</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	var _self;
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				value: false,
				PosterList: [],
				imgLists: [],
				page: {
					pagenum: 1,
					pagesize: 10
				}
			}
		},
		onShow() {
			// this.PosterList();
		},
		onLoad() {
			this.posterList();
			_self = this;
		},
		computed: {
			...mapState(['userInfo', 'hasLogin', 'scene', 'openid'])
		},
		methods: {
			goPosterSave(id) {
				uni.navigateTo({
					url: '/pageB/poster/poster-save/poster-save?id=' + id
				})
			},
			goeditimg(){
				
			},
			goPosterUpload() {
				uni.chooseImage({
					count: 1,
					sizeType: ['original'],
					success: function(res) {
						_self.uploadFiles(res.tempFilePaths)
					},
					fail: function() {
						// uni.hideLoading();
					}
				});
			},
			
			uploadFiles(f) {
				console.log(f)
				uni.uploadFile({
					url: this.serverUrl + 'app/upload',
					header: {
						"content-type": "application/x-www-form-urlencoded; charset=UTF-8"
					},
					filePath: f[0],
					name: 'file',
					method: "POST",
					formData: {
						username: "",
					},
					success: (r) => {
						let imgData = JSON.parse(r.data);
						if(imgData.code == 0){
							if(this.value){
								this.onImges(imgData)
							}else{
								// uni.redirectTo({
								// 	url: '/pages/poster/poster-save/poster-save?imgurlobj=' + JSON.stringify(imgData.fileName)
								// })
								// 修改图片尺寸
								uni.redirectTo({
									url: '/newpage/edithbimage/edit-image?imgurlobj=' + JSON.stringify(imgData.fileName)
								})
							}
						}else{
							console.log('请重新上传！')
						}
						
					},
					fail: (r) => {
						uni.showToast({
							title: "上传失败",
						})
					},
					complete: (r) => {
						console.log(r,'rrr-rrr')
					}
				});
			},
			onImges(imgData) {
				uni.request({
					url: this.serverUrl + 'app/addPoster',
					header: {
						"content-type": "application/x-www-form-urlencoded; charset=UTF-8"
					},
					method: 'POST',
					data: {
						memberId: this.userInfo.id,
						cover:imgData.fileName,
						pageSize: this.page.pagesize,
						pageNum: this.page.pagenum
					},
					success: (r) => {
						if(r.data.code == 1){
							// 修改图片尺寸
							uni.redirectTo({
								url: '/newpage/edithbimage/edit-image?imgurlobj=' + JSON.stringify(imgData.fileName)
							})
							// uni.redirectTo({
							// 	url: '/pages/poster/poster-save/poster-save?imgurlobj=' + JSON.stringify(imgData.fileName)
							// })
						}
					}
				})
			},
			change(e) {
				console.log('change', e);
			},

			posterList: function() {
				uni.request({
					url: this.serverUrl + 'app/getPosterList',
					header: {
						"content-type": "application/x-www-form-urlencoded; charset=UTF-8"
					},
					method: 'POST',
					data: {
						memberId: this.userInfo.id,
						pageSize: this.page.pagesize,
						pageNum: this.page.pagenum
					},
					success: (r) => {
						if (r.data.code == 0) {
							this.siteList = r.data.data
							// this.newsList = r.data.data
						}
					}
				})
			}
		}
	}
</script>

<style>
	.padding{
		padding: 30rpx;
	}
	.tj_box {
		display: flex;
		align-items: center;
	}
	.solids-bottom{
		margin-top: 40rpx;
	}
</style>
